package com.fh.qqserver.service;

import java.net.Socket;
import java.util.HashMap;
import java.util.Iterator;

/**
 * @author 方小勤
 * @version 1.0~
 * 该类用于管理和客户端通信的线程
 */
public class ManageClientThreads {
    private static HashMap<String,ServerConnectClientThread> serverConnectClientThreadHashMap = new HashMap<>();
    //返回 serverConnectClientThreadHashMap
    public static HashMap<String,ServerConnectClientThread> getServerConnectClientThreadHashMap() {
        return serverConnectClientThreadHashMap;
    }
    //添加线程对象到 serverConnectClientThreadHashMap 集合
    public static void addClientThread(String userId,ServerConnectClientThread serverConnectClientThread){
        serverConnectClientThreadHashMap.put(userId,serverConnectClientThread);
    }
    //根据 userId 返回 ServerConnectClientThread 线程
    public static ServerConnectClientThread getServerConnectClientThread(String userId){
        return serverConnectClientThreadHashMap.get(userId);
    }
    //增加一个方法，从集合中移除某个线程对象
    public static void removeClientThread(String userId){
        serverConnectClientThreadHashMap.remove(userId);
    }
    //编写一个方法，返回在线用户列表
    public static String getOnlineUser(){
        //集合遍历，遍历 hashmap 的key
        Iterator<String> iterator = serverConnectClientThreadHashMap.keySet().iterator();
        String onlineUser = "";
        while (iterator.hasNext()){
            onlineUser = onlineUser + iterator.next().toString() + " ";
        }
        return onlineUser;
    }
}
